﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CF_DAL_WS.Helpers
{
    public class MailHelper
    {
        public static bool SendingMail(string subject, string emailContent, string to)
        {
            bool result = false;
            try
            {
                GlobalVariables.Logger.Info("Start SendingEmail");
                GlobalVariables.Logger.Info("\tWebConfiguration.EmailLienHe: " + WebConfiguration.EmailLienHe);
                GlobalVariables.Logger.Info("\tWebConfiguration.PasswordEmailLienHe: " + WebConfiguration.PasswordEmailLienHe);
                GlobalVariables.Logger.Info("\tsubject: " + subject);
                GlobalVariables.Logger.Info("\temailContent: " + emailContent);
                GlobalVariables.Logger.Info("\tto: " + to);
                System.Net.Mail.SmtpClient mailclient = new System.Net.Mail.SmtpClient();
                System.Net.NetworkCredential auth = new System.Net.NetworkCredential(WebConfiguration.EmailLienHe, WebConfiguration.PasswordEmailLienHe);
                mailclient.Host = WebConfiguration.MailServer;
                mailclient.EnableSsl = true;
                mailclient.UseDefaultCredentials = true;
                mailclient.Credentials = auth;
                System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(WebConfiguration.EmailLienHe, to);
                mm.Subject = subject;
                mm.IsBodyHtml = true;
                mm.Body = emailContent;
                mailclient.Send(mm);
                result = true;
            }
            catch(Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
                result = false;
            }
            return result;
        }
    }
}